using System;
using System.Collections.Generic;
using iPhOwa.Exchange.ContentClasses;
using iPhOwa.Exchange.ContentClasses.Fields;
using iPhOwa.Exchange.WebDav;
using iPhOwa.Exchange.WebDav.Methods;

namespace iPhOwa.Exchange.Managers
{
	/// <summary>
	/// Encapsulates working with asppointments.
	/// </summary>
	public static class AppointmentManager
	{
		/// <summary>
		/// Get a list of appointments.
		/// </summary>
		public static ICollection<Appointment> GetList(Uri path, OwaSession session)
		{
			var properties = new[]
			                 	{
			                 			ItemFields.Href
			                 			, MessageFields.NormalizedSubject
			                 			, AppointmentFields.AllDayEvent
			                 			, AppointmentFields.DateStart
			                 			, AppointmentFields.DateEnd
			                 			, AppointmentFields.Location
			                 	};

			var search = new Search(path, session, properties);
			search.OrderBys.Add(new OrderBy(AppointmentFields.DateStart));
			search.Wheres.Add(new Where(AppointmentFields.DateEnd, ConditionalOperator.GreaterThan, DateTime.Now.Subtract(DateTime.Now.TimeOfDay)));

			var items = new List<Item>(search.Run());

			return items.ConvertAll(i => i as Appointment);
		}

		/// <summary>
		/// Get a specific appointment.
		/// </summary>
		public static Appointment Get(Uri path, OwaSession session)
		{
			var properties = new[]
			                 	{
			                 			MessageFields.TextDescription
			                 			, ItemFields.Href
			                 			, MessageFields.NormalizedSubject
			                 			, AppointmentFields.AllDayEvent
			                 			, AppointmentFields.DateStart
			                 			, AppointmentFields.DateEnd
			                 			, AppointmentFields.Location
			                 			, ItemFields.ParentName
			                 	};

			var propFind = new PropFind(path, session, properties);

			return (Appointment)propFind.Run();
		}
	}
}